package com.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dstats.R;
import com.entities.Twitch;
import com.fetch.ImageLoader;

public class TwitchAdapter extends BaseAdapter {
	private List<Twitch> streams;
	private LayoutInflater mInflater;
	private static ImageLoader imageLoader;
	static class ViewHolder{
		TextView title;
		TextView author;
		ImageView img;
		TextView views;
	}
	
	public TwitchAdapter(Context context, List<Twitch> news){
		mInflater = LayoutInflater.from(context);
		this.streams = news;
		if(imageLoader==null)
		imageLoader = new ImageLoader(context);
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return streams.size();
	}

	@Override
	public Object getItem(int arg0) {
		return streams.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		
		return streams.get(arg0).getId();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.news_layout, null);
			
			holder = new ViewHolder();
			holder.title = (TextView) convertView.findViewById(R.id.textViewNewsTitle);
			holder.author = (TextView) convertView.findViewById(R.id.textViewAuthor);
			holder.img = (ImageView) convertView.findViewById(R.id.imageViewNewsImage);
			holder.views = (TextView) convertView.findViewById(R.id.textViewVideoSentTimeAgo);
			convertView.setTag(holder);
			
		}else{
			holder = (ViewHolder) convertView.getTag();
		}

		Twitch stream = streams.get(position);
		holder.title.setText(stream.getStatus());
		holder.title.setTextColor(Color.BLACK);		   
		imageLoader.DisplayImage(stream.getPreview(), holder.img);
		holder.author.setText(stream.getDisplay_name());
		holder.views.setText(String.valueOf(stream.getViewers()));
	
		return convertView;
	}

}
